package com.rzdata.poct.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.rzdata.poct.common.core.mybatisplus.core.ServicePlusImpl;
import com.rzdata.poct.common.core.page.PagePlus;
import com.rzdata.poct.common.core.page.TableDataInfo;
import com.rzdata.poct.common.utils.PageUtils;
import com.rzdata.poct.common.utils.SecurityUtils;
import com.rzdata.poct.common.utils.StringUtils;
import com.rzdata.poct.system.domain.business.bo.PoctDetectionHandleBo;
import com.rzdata.poct.system.domain.business.po.PoctDetectionHandle;
import com.rzdata.poct.system.domain.business.po.PoctDetectionResult;
import com.rzdata.poct.system.domain.business.vo.PoctDetectionHandleVo;
import com.rzdata.poct.system.mapper.PoctDetectionHandleMapper;
import com.rzdata.poct.system.service.IPoctDetectionHandleService;
import com.rzdata.poct.system.service.IPoctDetectionResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * poct 检测处理Service业务层处理
 *
 * @author ruoyi
 * @date 2021-09-09
 */
@Service
public class PoctDetectionHandleServiceImpl extends ServicePlusImpl<PoctDetectionHandleMapper, PoctDetectionHandle, PoctDetectionHandleVo> implements IPoctDetectionHandleService {

    @Autowired
    IPoctDetectionResultService iPoctDetectionResultService;

    @Override
    public PoctDetectionHandleVo queryById(Long id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PoctDetectionHandleVo> queryPageList(PoctDetectionHandleBo bo) {
        PagePlus<PoctDetectionHandle, PoctDetectionHandleVo> result =
            pageVo(PageUtils.buildPagePlusNew(Long.valueOf(bo.getPageNum()), Long.valueOf(bo.getPageSize())), buildQueryWrapper(bo));
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<PoctDetectionHandleVo> queryList(PoctDetectionHandleBo bo) {
        return listVo(buildQueryWrapper(bo));
    }

    private LambdaQueryWrapper<PoctDetectionHandle> buildQueryWrapper(PoctDetectionHandleBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PoctDetectionHandle> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDetectionId() != null, PoctDetectionHandle::getDetectionId, bo.getDetectionId());
        lqw.eq(StringUtils.isNotBlank(bo.getReson()), PoctDetectionHandle::getReson, bo.getReson());
        lqw.eq(StringUtils.isNotBlank(bo.getOpinion()), PoctDetectionHandle::getOpinion, bo.getOpinion());
        lqw.eq(StringUtils.isNotBlank(bo.getProcess()), PoctDetectionHandle::getProcess, bo.getProcess());
        lqw.eq(bo.getProcessId() != null, PoctDetectionHandle::getProcessId, bo.getProcessId());
        lqw.eq(bo.getStauts() != null, PoctDetectionHandle::getStauts, bo.getStauts());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PoctDetectionHandleBo bo) {
        PoctDetectionHandle add = BeanUtil.toBean(bo, PoctDetectionHandle.class);
        add.setCreateTime(new Date());
        add.setHandUserId(SecurityUtils.getUserId());
        add.setHandUserName(SecurityUtils.getUsername());
        validEntityBeforeSave(add);
        // 更新检验状态
        PoctDetectionResult poctDetectionResult = new PoctDetectionResult();
        poctDetectionResult.setId(bo.getDetectionId());
        poctDetectionResult.setStatus(1);
        iPoctDetectionResultService.updateById(poctDetectionResult);
        return save(add);
    }

    @Override
    public Boolean updateByBo(PoctDetectionHandleBo bo) {
        PoctDetectionHandle update = BeanUtil.toBean(bo, PoctDetectionHandle.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PoctDetectionHandle entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
